package thread;
//两个线程两把锁的死锁情况
public class ThreadDemo20 {
    public static void main(String[] args) {

        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(()->{
            synchronized (A){
                try {//sleep以下,是给t2一些时间,让t2也能拿到B
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //不释放A获取B
                synchronized (B){
                    System.out.println("t1拿到了两把锁");
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (B){
                try {//sleep以下,是给t1一些时间,让t1也能拿到A
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //不释放B获取A
                synchronized (A){
                    System.out.println("t2拿到了两把锁");
                }
            }
        });
        t1.start();
        t2.start();

    }
}
